package webserver;

import java.net.*;
import java.util.logging.Logger;

/**
 *
 * @author lucas
 */
public class Server {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        me = new Server();
        me.work();
    }
    
    @Override
    public void finalize(){
        log.info("FINALIZE");
    }
    
    /**
     * devuelve la instancia de esta clase
     * 
     * @return 
     */
    public static Server getInstance() {
        return me;
    }
    
    /**
     * constructor
     */
    public Server() {
    }
    
    /**
     * worker
     */
    public void work() {
        ServerSocket server = null;
        try {
            server = new ServerSocket(Global.SERVER_SOCKET);
            server.setSoTimeout(0);
        } catch (Exception ex) {
            log.severe(ex.getMessage());
        }
        while (true) {
            log.info(String.format("HTTP Server Waiting for client on port %d", Global.SERVER_SOCKET));
            Socket socket = null;
            try {
                socket = server.accept();
            } catch (Exception ex) {
                log.severe(ex.getMessage());
            }
            if (client != null) {
                client.kill();
            }
            client = new Client(socket);
        }
    }
    
    /***************************************************************************
     * Variables privadas
     */
    private static Server me = null;
    private Logger log = Logger.getAnonymousLogger();
    private Client client = null;
}
